/*
 * $Id$
 *
 * Copyright (C) IUT Montreuil-University Paris 8, 2009-2010
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

package fr.univ_paris8.iut.linc.align;




import java.net.URI;
import java.util.Properties;

import org.semanticweb.owl.align.Alignment;
import org.semanticweb.owl.align.AlignmentException;
import org.semanticweb.owl.align.AlignmentProcess;
import org.semanticweb.owl.align.Cell;
import org.semanticweb.owl.align.Parameters;

import ca.uqam.info.latece.sboa.impl.algorithms.OLAlignment;

//import ca.uqam.info.latece.sboa.impl.algorithms.OLAlignment;

import fr.inrialpes.exmo.align.impl.BasicAlignment;
import fr.inrialpes.exmo.align.impl.BasicParameters;


import fr.inrialpes.exmo.align.impl.edoal.ClassExpression;
import fr.inrialpes.exmo.align.impl.edoal.ClassId;
import fr.inrialpes.exmo.align.impl.edoal.EDOALAlignment;
import fr.inrialpes.exmo.align.impl.edoal.EDOALCell;
import fr.inrialpes.exmo.align.impl.edoal.EDOALRelation;
import fr.inrialpes.exmo.align.impl.edoal.PropertyExpression;
import fr.inrialpes.exmo.align.impl.edoal.PropertyId;
import fr.inrialpes.exmo.align.ling.JWNLAlignment;
import fr.inrialpes.exmo.aroma.AROMA;


import fr.inrialpes.exmo.ontosim.string.JWNLDistances;
import fr.inrialpes.exmo.ontowrap.Ontology;


public class SimpleAlignment {
	
	/**
     * SimpAlignment est l'alignement simple qui est généré par la combinaison de trois aligneurs classiques
     */
	public static EDOALAlignment SimpAlignment;
	
	public int Align(URI onto1, URI onto2,Ontology o1, Ontology o2, String f, String wndict) {
		
		SimpAlignment = new  EDOALAlignment();
		Properties params = new BasicParameters();
		Parameters params2 = new BasicParameters();
		JWNLDistances jDist = null;
	    jDist = new JWNLDistances();
		jDist.Initialize( wndict, "3.0" );
		
		
		
		try {
		
		/**
	     * Aligner les deux ontologies en combinant trois aligneurs simples
	     */
	    
		//Aligneur 1 : Permet d'exploiter la ressource WordNet
		BasicAlignment a1 = new JWNLAlignment();
	    a1.init ( onto1, onto2 );
	    ((AlignmentProcess) a1).align((Alignment)null, params);
	    a1.cut(.7); 
	    
	   
	    //Aligneur 2 : OLA qui est fondé sur la structure
	    
	    /*
	    OLAlignment a2 = new OLAlignment();
	    a2.init ( o1, o2 );
	    a2.align( (Alignment)null, params );
	    a2.cut(.7); 
	            */
	    
	    //Aligneur 3 : AROMA qui est fondé sur les annotations des entités
	    
	    /*
	    AROMA a3= new AROMA ();
	    a3.init ( onto1, onto2 );
	    a3.align( (Alignment)null, params2 );
	    a3.cut(.7); 
	    */
	    
	    SimpAlignment.init ( o1, o2 );
	    
	    SimpAlignment = AddAlignment (a1,SimpAlignment ); 
	    //SimpAlignment = AddAlignment (a2, SimpAlignment);
	    // SimpAlignment = AddAlignment (a3, SimpAlignment);
	    SimpAlignment = filter (SimpAlignment, o1, o2);   
	  
	    /*
	   PrintWriter writer = new PrintWriter (
				  new BufferedWriter(
		                   new OutputStreamWriter( System.out, "UTF-8" )), true);
	    AlignmentVisitor renderer = new RDFRendererVisitor(writer);
	    a2.render(renderer);
	    writer.flush();
	    writer.close();*/
	    
	} 
	
	catch (Exception e) { e.printStackTrace(); };

	return SimpAlignment.nbCells();
}
	
	/**
     * Conserver que les correspondances ayant la meilleur mesure de confiance
     */

	private EDOALAlignment filter (EDOALAlignment a, Ontology onto1, Ontology onto2) throws AlignmentException{
		EDOALAlignment res = new EDOALAlignment();
		EDOALCell ec;
		res.init(onto1, onto2);
		boolean trouve;
		
		
		
		for (Cell c2 : (a.getArrayElements())) {
			
			
			java.util.Iterator<Cell> it = res.iterator();
			trouve = false;
			while ((it.hasNext())&&(!trouve)){
				
				Cell c1 = it.next();
				if ((c1.getObject1AsURI(res).equals(c2.getObject1AsURI(a)))&&
				(c1.getObject2AsURI(res).equals(c2.getObject2AsURI(a)))&&
				c1.getRelation().equals(c2.getRelation())){
					trouve = true;
					if (c2.getStrength()> c1.getStrength())						
						c1.setStrength(c2.getStrength());
				}	
				
				}
			
			if (!trouve)	{
				
				if (c2.getObject1().getClass().toString().contains("Class")){
				
					ClassExpression ce1 = new ClassId(c2.getObject1AsURI(a));
					ClassExpression ce2 = new ClassId(c2.getObject2AsURI(a));
					EDOALRelation r = new EDOALRelation(c2.getRelation().getRelation());
					ec = new EDOALCell (null, ce1,ce2,r,c2.getStrength());
					
				
				}
				else
				{
					PropertyExpression pe1 = new PropertyId(c2.getObject1AsURI(a));
					PropertyExpression pe2 = new PropertyId(c2.getObject2AsURI(a));
					EDOALRelation r = new EDOALRelation(c2.getRelation().getRelation());
					ec = new EDOALCell (null, pe1,pe2,r,c2.getStrength());
				}
				
				res.addAlignCell(ec);
			}
		}
		
		
		
		return res;
	
}
	
	/**
     * Mélanger l'ensemble de correspondances de deux alignements simples
     */
	
	public EDOALAlignment AddAlignment (BasicAlignment a1, EDOALAlignment a2) throws AlignmentException {
		
		EDOALCell ec;
		
		for (Cell c :   a1.getArrayElements() ){
						
			if (c.getObject1().getClass().toString().contains("Class")){
				ClassExpression ce1 = new ClassId(c.getObject1AsURI(a1));
				ClassExpression ce2 = new ClassId(c.getObject2AsURI(a1));
				EDOALRelation r = new EDOALRelation(c.getRelation().getRelation());
			    ec = new EDOALCell (null, ce1,ce2,r,c.getStrength());
			    
			}
			else 
			{ 
				PropertyExpression ce1 = new PropertyId(c.getObject1AsURI(a1));
				PropertyExpression ce2 = new PropertyId(c.getObject2AsURI(a1));
				EDOALRelation r = new EDOALRelation(c.getRelation().getRelation());
			    ec = new EDOALCell (null, ce1,ce2,r,c.getStrength());
			    
			}
			
			a2.addAlignCell(ec);
		}
		
		return a2;
	}
}
